package com.hadoopSpark.services.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.configuration.ConfigurationTool;
import com.aliyun.util.String2JsonTool;
import com.certificate.tool.SetTrustStore;
import com.common.constant.Constant;

/*
s * function: [列出所有服务实例的列表]
 * 		查看服务实例列表： 查看用户在容器集群中创建的所有服务实例列表
 * 就是说，比方一个应用里有N个服务，如MR服务，spark服务，hive服务，
 * 就是查看这些的
 */
public class ListAllServices {
	public static void main(String[] args) {
		
		String clustername = args[0];
		

		// get certificate address
		try {
			SSLConnectionSocketFactory sslsf = SetTrustStore.getSSLConnection();
			// httpclient链接
			CloseableHttpClient httpClient = HttpClients.custom()
					.setSSLSocketFactory(sslsf).build();
			//args[0] = hadoop8_master
			HttpGet httpGet = new HttpGet(
					ConfigurationTool.getProperties(Constant.ALIYUN_SERVICES_URL));
			
			// 使用客户端向服务器发送数据
			HttpResponse httpResponse = httpClient.execute(httpGet);

			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				//输出协议，响应码，结果
//				System.out.println(httpResponse.getProtocolVersion() + " "
//						+ httpResponse.getStatusLine().getStatusCode() + " "
//						+ httpResponse.getStatusLine().getReasonPhrase());
				
				HttpEntity resultEntity = httpResponse.getEntity();
				
				//返回结果是，服务器上，返回来的是json字符串数据
				String strResultEntity = EntityUtils.toString(resultEntity);
				
				String2JsonTool.printJson(strResultEntity);
				
				String result = String2JsonTool.formatJson(strResultEntity);
				
				
				JSONObject resultJSON = JSONObject.parseObject(result);
				
				JSONArray clusternameArray1 =  JSONObject.parseArray(result);
				
				System.out.println(clusternameArray1);
				
//				JSONArray clusternameArray =  resultJSON.getJSONArray(clustername);
//				
//				System.out.println("clustername:" + clusternameArray.getString(0));
				
//				System.out.println("OK!!!");
			} else {
				
				//输出协议，响应码，结果
				System.out.println(httpResponse.getProtocolVersion() + " "
						+ httpResponse.getStatusLine().getStatusCode() + " "
						+ httpResponse.getStatusLine().getReasonPhrase());
				
				String bodyAsString = EntityUtils.toString(httpResponse
						.getEntity());
				
				String2JsonTool.printJson(bodyAsString);
				
				
				
				System.out.println("Something is wrong!!!");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
		
	
	
	
	
	
	
	
